<style>
    .layui-form-onswitch {
        border-color: #5FB878;
        background-color: #5FB878;
    }
</style>
<form class="layui-form seller-form" action="" method="post">
    <div class="layui-form-item">
        <div class="layui-inline">
            <input type="text" name="username" lay-verify="title" style="width:200px;"
                 class="layui-input" placeholder="Username">
        </div>
        <div class="layui-inline">
            <input type="text" name="business_token" lay-verify="title" style="width:200px;"
                class="layui-input" placeholder="Business Token">
        </div>
        <div class="layui-inline">
            <input type="text" name="user_memo" lay-verify="title" style="width:200px;"
                       class="layui-input" placeholder="User Memo">
        </div>
        <div class="layui-inline" style="">
            <select name="user_type" lay-verify="" >
                <option value="">please select User Type</option>
                <option value="0">Single</option>
                <option value="1">Share</option>
            </select>
        </div>
        <div class="layui-inline" style="padding-left:20px;">
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="user-search"><i
                    class="iconfont icon-chaxun"></i>Search</button>
        </div>
    </div>
</form>

<div class="table-body">
    <table id="userTable" lay-filter="test"></table>
</div>

<script>
    layui.use(['form', 'table' , 'laydate'], function () {

        //时间插件
        layui.laydate.render({
            elem: '#datetime',
            range: '_',
            format: 'yyyy-MM-dd'
        });

        var tableIn = layui.table.render({
            elem: '#userTable',
            height: 'full-139',
            cellMinWidth: '80',
            totalRow: true,
            toolbar: '#toolbar', //开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: ['exports'],
            url: "{:url('monthDebt')}?_ajax=1",
            id: 'userTable',
            cols: [[
                // {type:'checkbox'},
                { type: 'numbers'},
                { field: 'username', align: 'left', style:'font-weight:bold;' ,width:200, title: 'User Name'},
                { field: 'user_type', title: 'UserType', width:180 ,
                    templet: function (d) {
                        const user_type = d.user_type;
                        if(user_type == 1){
                            return '<font color="red">Share</font>';
                        }else{
                            return 'Single';
                        }
                    }
                },
                { field: 'business_token', align: 'left',  title: 'Business Token'},
                { field: 'user_num', align: 'center', title: 'Memo',width:180},
                { field: 'balance', title: 'Balance($)', align:"right",width:180,
                    style:'color:#FF5722;font-weight:bold'},
                { field: 'month_fee', title: 'MonthFee($)', align:"right",width:180,
                    style:'color:#FF5722;font-weight:bold'},
                { field: 'dept', title: 'MonthDept($)', align:"right",width:180,
                    templet: function (d) {
                        const debt = d.debt;
                        if(debt > 0){
                            return '<font color="red">' + debt + '</font>';
                        }else{
                            return '';
                        }
                    },totalRow: true
                },
                { field: 'check_time', align: 'center', title: 'Active Time' , width: 200,
                    templet: function (d) {
                        return layui.util.toDateString(d.check_time * 1000, "yyyy-MM-dd HH:mm:ss");
                    }
                },
                { field: 'option', title: 'Action', templet: function (d) {
                    var html = '<a class="layui-btn layui-btn-xs editDebt" data-id="' + d.id + '">editDebt</a>';
                    return html;
                }, align: 'center', width: 100}
            ]],
            page: true,
            limit: 20,
            limits: [20, 50, 100 , 500 ,1000],
        });
        //搜索
        layui.form.on('submit(user-search)', function (data) {
            layui.table.reload('userTable', {
                where: data.field
                , page: {
                    curr: 1 //重新从第 1 页开始
                },
                method:'POST'
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        $(document).on('click', '.editDebt', function () {
            var id = $(this).attr('data-id');
            layer.confirm('Are you sure?', {
                title:'Window' , btn: ['confirm', 'cancel'], //按钮
            }, function () {
                JsPost("{:url('editDebt')}", {user_id:id}, function(res){
                    if(res.code >0){
                        layer.msg(res.msg, { time: 1500 , icon:1}, function () {
                            tableIn.reload({
                                page:{
                                    curr :1
                                }
                            });
                        });
                    }else{
                        layer.msg(res.msg , {time:1500 , icon:2});
                    }
                })
            }, function () {
                tableIn.reload({
                    page:{
                        curr :1
                    }
                });
            });
        });
    });
</script>